package admin.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.StyleContext.SmallAttributeSet;

public class AdminLogin extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AdminLogin() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = response.getWriter();
		ServletContext application=getServletContext();
		Connection con=(Connection) application.getAttribute("con");
		Statement sm=null;
		try {
			sm=con.createStatement();
		} catch (SQLException e) {
			log.log.err(e.toString());
			return ;
		}
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		String sqls1="SELECT * FROM admin WHERE username='"+username+"' AND PASSWORD='"+password+"'";
		ResultSet rs=null;
		try {
			rs=sm.executeQuery(sqls1);
		} catch (SQLException e) {
			log.log.err(e.toString());
			return ;
		}
		try {
			rs.last();
			int len=rs.getRow();
			if(len==1) out.print("succes");
			else out.print("failed");
		} catch (Exception e) {
			log.log.err(e.toString());
			return ;
		}
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
